/*
* ----------------------------------------------------------------------------
* 鈥淭HE COFFEEWARE LICENSE鈥� (Revision 1):
* <ihsan@kehribar.me> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a coffee in return.
* -----------------------------------------------------------------------------
* Please define your platform spesific functions in this file ...
* -----------------------------------------------------------------------------
*/


#include "debug.h"
#include "nrf24.h"
/* ------------------------------------------------------------------------- */
// if u want to change Sensor2 to Sensor1, it is required that change cs and ce pins (PA8, PA5 --> PA15, PA0).
// it should be noticed to change IRQ pin from PB4 to PA1
// Do not change SCK, MOSI, and MISO.
void nrf24_setupPins(uint8_t sensor)
{
	switch(sensor)
	{
	case SENSOR_CLICK_1:
	    GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1); /* CS */
	    GPIO_WriteBit(GPIOA, GPIO_Pin_0, 1); /* CE */
		break;
	case SENSOR_CLICK_2:
	    GPIO_WriteBit(GPIOA, GPIO_Pin_8, 1); /* CS */
	    GPIO_WriteBit(GPIOA, GPIO_Pin_5, 1); /* CE */
		break;
	default:
		break;
	}
#ifdef SOFT_SPI
    GPIO_SetBits(GPIOB, GPIO_Pin_13); /* SCK */
    GPIO_SetBits(GPIOB, GPIO_Pin_15); /* MOSI */
    GPIO_ResetBits(GPIOB, GPIO_Pin_14); /* MISO */
#endif
}
/* ------------------------------------------------------------------------- */
void nrf24_ce_digitalWrite(uint8_t sensor, uint8_t state)
{
    if(state)
    {
    	switch(sensor)
    	{
    	case SENSOR_CLICK_1:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_0, 1); /* CE */
    		break;
    	case SENSOR_CLICK_2:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_5, 1); /* CE */
    		break;
    	default:
    		break;
    	}
    }
    else
    {
    	switch(sensor)
    	{
    	case SENSOR_CLICK_1:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_0, 0); /* CE */
    		break;
    	case SENSOR_CLICK_2:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_5, 0); /* CE */
    		break;
    	default:
    		break;
    	}
    }
}
/* ------------------------------------------------------------------------- */
void nrf24_csn_digitalWrite(uint8_t sensor, uint8_t state)
{
    if(state)
    {
    	switch(sensor)
    	{
    	case SENSOR_CLICK_1:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1); /* CS */
    		break;
    	case SENSOR_CLICK_2:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_8, 1); /* CS */
    		break;
    	default:
    		break;
    	}
    }
    else
    {
    	switch(sensor)
    	{
    	case SENSOR_CLICK_1:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0); /* CS */
    		break;
    	case SENSOR_CLICK_2:
        	GPIO_WriteBit(GPIOA, GPIO_Pin_8, 0); /* CS */
    		break;
    	default:
    		break;
    	}
    }
}
#ifdef SOFT_SPI
/* ------------------------------------------------------------------------- */
void nrf24_sck_digitalWrite(uint8_t state)
{
    if(state)
    {
        GPIO_SetBits(GPIOB, GPIO_Pin_13);
    }
    else
    {
        GPIO_ResetBits(GPIOB, GPIO_Pin_13);
    }
}
/* ------------------------------------------------------------------------- */
void nrf24_mosi_digitalWrite(uint8_t state)
{
    if(state)
    {
        GPIO_SetBits(GPIOB, GPIO_Pin_15);
    }
    else
    {
        GPIO_ResetBits(GPIOB, GPIO_Pin_15);
    }
}
/* ------------------------------------------------------------------------- */
uint8_t nrf24_miso_digitalRead()
{
    return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
}
/* ------------------------------------------------------------------------- */
#endif
